package com.mlming.springboot.service.impl;

import com.mlming.springboot.dao.CardMapper;
import com.mlming.springboot.enums.ResponseEnum;
import com.mlming.springboot.form.CardForm;
import com.mlming.springboot.pojo.Card;
import com.mlming.springboot.pojo.Comment;
import com.mlming.springboot.service.CardService;
import com.mlming.springboot.service.CommentService;
import com.mlming.springboot.vo.CardVo;
import com.mlming.springboot.vo.ResponseVo;
import com.mlming.springboot.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.InetAddress;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private CommentService commentService;

    @Override
    public ResponseVo addCard(CardForm cardForm) {
        Card card = new Card();
        int res = cardMapper.insertSelective(card);
        if(res == 0) return ResponseVo.error(ResponseEnum.ERROR);
        return ResponseVo.success();
    }

    @Override
    public ResponseVo<List<CardVo>> getCardList() {
        // 查出所有的卡片列表cards
        List<Card> cards = cardMapper.selectAll();
        // 查出所有的评论:
        List<Comment> comments = commentService.getAllCommentList();
        // 将评论列表转为Map<CardId,List<Comment>>
        Map<Integer,List<Comment>> commentMap = comments.stream().collect(Collectors.groupingBy(Comment::getCardId));
        // 将一个个card与对应的List<Comment>绑定
        List<CardVo> cardVos = new ArrayList<>();
        for (Card card : cards) {
            // 构建一个个cardVo
            CardVo cardVo = new CardVo();
            BeanUtils.copyProperties(card,cardVo);
            List<Comment> curComments = commentMap.get(card.getId());
            cardVo.setCommentList(curComments);
            // 加入最终列表
            cardVos.add(cardVo);
        }
        return ResponseVo.success(cardVos);
    }

    @Override
    public ResponseVo<CardVo> getCard(Integer id) {
        // 获取到id对应的Card
        Card card = cardMapper.selectByPrimaryKey(id);
        // 获取到对应的评论列表, 根据业务要求反转一下
        List<Comment> comments = commentService.getCommentListOfCard(id);
        Collections.reverse(comments);
        // 构建cardVo
        CardVo cardVo = new CardVo();
        BeanUtils.copyProperties(card,cardVo);
        cardVo.setCommentList(comments);

        return ResponseVo.success(cardVo);
    }

    @Override
    public ResponseVo addPraise(Integer id, Integer praises) {
        // 获取到id对应的Card
        Card card = cardMapper.selectByPrimaryKey(id);
        card.setPraises(String.valueOf(praises + 1));
        // 更新数据库
        int res = cardMapper.updateByPrimaryKeySelective(card);
        if(res == 0) return ResponseVo.error(ResponseEnum.ERROR);
        return ResponseVo.success();
    }

    @Override
    @Transactional
    public ResponseVo addComments(Integer id, Comment comment) {
        // 获取到id对应的Card
        Card card = cardMapper.selectByPrimaryKey(id);
        card.setComments(String.valueOf(Integer.valueOf(card.getComments()) + 1));
        // 把comment插入到comment表里
        ResponseVo responseVo = commentService.addComment(comment);
        if(responseVo.getStatus() != ResponseEnum.SUCCESS.getCode()) ResponseVo.error(ResponseEnum.ERROR);
        // 更新card的评论数
        int res = cardMapper.updateByPrimaryKeySelective(card);
        if(res == 0) return ResponseVo.error(ResponseEnum.ERROR);
        return ResponseVo.success();
    }
}
